package cn.kailangtu.dataspot.data.context;

import android.graphics.Color;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.kailangtu.commonevents.data.DataBean;
import cn.kailangtu.commonevents.eventEntity.KeyboardSelectionCloseEvent;
import cn.kailangtu.dataspot.R;

public class KeyboardSelectContext {
    public static List<DataBean<Integer>> selectItem ;
    public static int selectIndex = -1;
    public static DataBean result = null;
    private  static TextView selectedView;
    private static int normalColor;

    public static TextView getSelectedView()
    {
        return selectedView;
    }

    public static void setSelectedView(TextView newView)
    {

        if(selectedView != null)
        {
            if(selectedView.getTag().equals("left"))
            {
                selectedView.setBackgroundResource(R.drawable.log_go_left);
            }
            else if(selectedView.getTag().equals("right"))
            {
                selectedView.setBackgroundResource(R.drawable.log_go_right);
            }
            else if(selectedView.getTag().equals("leftSmall"))
            {
                selectedView.setBackgroundResource(R.drawable.keyboard_select_btn_left_normal);
            }
            else if(selectedView.getTag().equals("rightSmall"))
            {
                selectedView.setBackgroundResource(R.drawable.keyboard_select_btn_right_normal);
            }
            else if(selectedView.getTag().equals("cancel")){
                selectedView.setBackgroundResource(R.drawable.button_selection_normal);
            }
            else if(selectedView.getTag().equals("save")){
                selectedView.setBackgroundResource(R.drawable.button_selection_normal);
            }
            selectedView.setTextColor(normalColor);
        }
        normalColor = newView.getCurrentTextColor();
        selectedView = newView;
        newView.setTextColor(Color.parseColor("#FF0000"));
        if(selectedView.getTag().equals("left"))
        {
            selectedView.setBackgroundResource(R.drawable.log_go_left_selected);
        }
        else if(selectedView.getTag().equals("right"))
        {
            selectedView.setBackgroundResource(R.drawable.log_go_right_selected);
        }

        else if(selectedView.getTag().equals("leftSmall"))
        {
            selectedView.setBackgroundResource(R.drawable.keyboard_select_btn_left_selected);
        }
        else if(selectedView.getTag().equals("rightSmall"))
        {
            selectedView.setBackgroundResource(R.drawable.keyboard_select_btn_right_selected);
        }
        else if(selectedView.getTag().equals("cancel")){
            selectedView.setBackgroundResource(R.drawable.button_selection_selected);
            selectedView.setTextColor(Color.parseColor("#FFFFFF"));
        }
        else if(selectedView.getTag().equals("save")){
            selectedView.setBackgroundResource(R.drawable.button_selection_selected);
            selectedView.setTextColor(Color.parseColor("#FFFFFF"));
        }
    }

    public static void clean()
    {
        selectIndex = -1;
        result = null;
        selectItem = null;
    }

    public static void reset()
    {
        if(selectItem != null && result != null)
        {
            for(int i =0;i< selectItem.size();i++){
                if(selectItem.get(i).getLabel().equals(result.getLabel()))
                {
                    selectIndex = i;
                    break;
                }
            }
        }
    }


    public static String goFirst(){
        if(selectItem != null && selectItem.size() >0){
            selectIndex = 0;
            result = selectItem.get(selectIndex);
            return result.getLabel();
        }
        return "";
    }

    public static boolean goLeft()
    {
        if(selectItem != null && selectItem.size()>0) {
            if (selectIndex > 0) {
                selectIndex--;
                if ( selectItem.size() > selectIndex) {
                    result = selectItem.get(selectIndex);
                }
            }else{
                selectIndex = selectItem.size() -1;
                result = selectItem.get(selectIndex);
            }
        }
        return false;
    }

    public static boolean goRight(){
        if( selectItem != null && selectItem.size()>0 )
        {
            if(selectIndex < selectItem.size()-1)
            {
                selectIndex++;
                result = selectItem.get(selectIndex);
            }
            else
            {
                selectIndex = 0;
                result = selectItem.get(selectIndex);
            }
        }
        return false;
    }

    public static void entry(){
        if(selectedView != null){
            if(selectedView.getTag().equals("left") || selectedView.getTag().equals("leftSmall"))
            {
                goLeft();
            }
            else if(selectedView.getTag().equals("right") || selectedView.getTag().equals("rightSmall"))
            {
                goRight();
            }
            else if(selectedView.getTag().equals("center"))
            {
                goRight();
            }
            else if(selectedView.getTag().equals("save"))
            {
                if(selectIndex > -1 && selectItem != null && selectItem.size() > selectIndex)
                {
                    result = selectItem.get(selectIndex);
                    EventBus.getDefault().post(new KeyboardSelectionCloseEvent(result,false));
                }
                else
                {
                    EventBus.getDefault().post(new KeyboardSelectionCloseEvent(null,true));
                }
            }
            else if(selectedView.getTag().equals("cancel")){
                EventBus.getDefault().post(new KeyboardSelectionCloseEvent(null,true));
            }
        }

    }
}
